package com.pearl.boot.web;

import com.pearl.boot.profile.DevProfile;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * @author TangDan
 * @version 1.0
 * @since 2022/10/8
 */
@DevProfile
@AutoConfiguration
@ConditionalOnProperty(
        prefix = "pearl.boot.web",
        name = {"enabled"},
        havingValue = "true",
        matchIfMissing = true
)
public class PearlWebAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "pearl.boot.web")
    public PearlWebProperties pearlWebProperties() {
        return new PearlWebProperties();
    }

    @Bean
    public FilterRegistrationBean<ContentCachingRequestWrapperFilter> contentCachingRequestWrapperFilter() {
        FilterRegistrationBean<ContentCachingRequestWrapperFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new ContentCachingRequestWrapperFilter());
        filterFilterRegistrationBean.setOrder(-1);
        filterFilterRegistrationBean.setName("contentCachingRequestWrapperFilter");
        return filterFilterRegistrationBean;
    }
}
